package com.jaya.budan.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.RequiresApi;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.google.gson.Gson;
import com.jaya.budan.R;
import com.jaya.budan.adapter.Fill_Information2_Adapter;
import com.jaya.budan.base.BaseActivity;
import com.jaya.budan.bean.User_Information;
import com.jaya.budan.connect.RequestConnect;
import com.jaya.budan.utils.RangeSeekBar;
import com.jaya.budan.utils.ToggleButton;
import com.jaya.budan.utils.ToolUtils;
import com.jaya.budan.utils.VolleyUtil;
import com.jaya.budan.views.MyGridView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class TiaoJian_ShaiXuan_Activity extends BaseActivity implements View.OnClickListener {

    private RelativeLayout back;
    private MyGridView gv_fill_item_type, gv_fill_item_juese, gv_fill_item_tixing;
    private TextView tv_fill_info2_juese, tv_fill_info2_type, tv_fill_info2_tixing, tv_feedback_tijiao;
    private int int_type = 0, int_juese = 0, int_tixing = 0;
    private JSONArray list_age = new JSONArray();
    private JSONArray list_height = new JSONArray();
    private JSONArray list_weight = new JSONArray();
    private String access_token;
    private boolean is_gaoji = true;
    private RangeSeekBar srb_shaixuan_age, srb_shaixuan_height, srb_shaixuan_weight;
    private ToggleButton tb_shaixuan;
    private ImageView iv_gaoji_shaixuan;
    private User_Information user_information;
    private Fill_Information2_Adapter fill_information2_adapter, fill_information2_adapter2, fill_information2_adapter3;
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tiaojian_shaixuan);
        Intent intent = getIntent();
        access_token = intent.getStringExtra("access_token");
        back = findViewById(R.id.back);
        srb_shaixuan_age = findViewById(R.id.srb_shaixuan_age);
        srb_shaixuan_height = findViewById(R.id.srb_shaixuan_height);
        srb_shaixuan_weight = findViewById(R.id.srb_shaixuan_weight);
        iv_gaoji_shaixuan = findViewById(R.id.iv_gaoji_shaixuan);
        tv_fill_info2_juese = findViewById(R.id.tv_fill_info2_juese);
        tv_fill_info2_type = findViewById(R.id.tv_fill_info2_type);
        tv_fill_info2_tixing = findViewById(R.id.tv_fill_info2_tixing);
        tv_feedback_tijiao = findViewById(R.id.tv_feedback_tijiao);
        gv_fill_item_type = findViewById(R.id.gv_fill_item_type);
        gv_fill_item_juese = findViewById(R.id.gv_fill_item_juese);
        gv_fill_item_tixing = findViewById(R.id.gv_fill_item_tixing);
        tb_shaixuan = findViewById(R.id.tb_shaixuan);
        back.setOnClickListener(this);
        tv_feedback_tijiao.setOnClickListener(this);
        iv_gaoji_shaixuan.setOnClickListener(this);
        postData();
        gv_fill_item_type.setVerticalSpacing(15);
        gv_fill_item_type.setHorizontalSpacing(15);
        gv_fill_item_juese.setVerticalSpacing(15);
        gv_fill_item_juese.setHorizontalSpacing(15);
        gv_fill_item_tixing.setVerticalSpacing(15);
        gv_fill_item_tixing.setHorizontalSpacing(15);
        gv_fill_item_type.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ToolUtils.showToast(TiaoJian_ShaiXuan_Activity.this, user_information.getType().get(position).getName());
                fill_information2_adapter.setSelect(position);
                fill_information2_adapter.notifyDataSetChanged();
                if (user_information.getType().get(position).getName().equals("弯男") ||
                        user_information.getType().get(position).getName().equals("弯女") ||
                        user_information.getType().get(position).getName().equals("双性恋") ){
                    tv_fill_info2_juese.setVisibility(View.VISIBLE);
                    tv_fill_info2_tixing.setVisibility(View.VISIBLE);
                    gv_fill_item_juese.setVisibility(View.VISIBLE);
                    gv_fill_item_tixing.setVisibility(View.VISIBLE);
//                    if (sex == 2){
//                        tv_fill_info2_tixing.setText("形象");
//                    }
                } else {
                    tv_fill_info2_juese.setVisibility(View.GONE);
                    tv_fill_info2_tixing.setVisibility(View.GONE);
                    gv_fill_item_juese.setVisibility(View.GONE);
                    gv_fill_item_tixing.setVisibility(View.GONE);
                }
                int_type = user_information.getType().get(position).getValue();
            }
        });
        gv_fill_item_juese.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ToolUtils.showToast(TiaoJian_ShaiXuan_Activity.this, user_information.getRole().get(position).getName());
                fill_information2_adapter2.setSelect(position);
                fill_information2_adapter2.notifyDataSetChanged();
                int_juese = user_information.getRole().get(position).getValue();
            }
        });
        gv_fill_item_tixing.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ToolUtils.showToast(TiaoJian_ShaiXuan_Activity.this, user_information.getFace().get(position).getName());
                fill_information2_adapter3.setSelect(position);
                fill_information2_adapter3.notifyDataSetChanged();
                int_tixing = user_information.getFace().get(position).getValue();
            }
        });
        srb_shaixuan_age.setOnRangeChangedListener(new RangeSeekBar.OnRangeChangedListener() {
            @Override
            public void onRangeChanged(RangeSeekBar view, float min, float max) {
                list_age = new JSONArray();
                list_age.put((int) min);
                list_age.put((int) max);
            }
        });
        srb_shaixuan_height.setOnRangeChangedListener(new RangeSeekBar.OnRangeChangedListener() {
            @Override
            public void onRangeChanged(RangeSeekBar view, float min, float max) {
                list_height = new JSONArray();
                list_height.put((int) min);
                list_height.put((int) max);
            }
        });
        srb_shaixuan_weight.setOnRangeChangedListener(new RangeSeekBar.OnRangeChangedListener() {
            @Override
            public void onRangeChanged(RangeSeekBar view, float min, float max) {
                list_weight = new JSONArray();
                list_weight.put((int) min);
                list_weight.put((int) max);
            }
        });
        tb_shaixuan.setToggleOn();//默认  为开启状态
        tb_shaixuan.setOnToggleChanged(new ToggleButton.OnToggleChanged() {
            @Override
            public void onToggle(boolean on) {

            }
        });
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_feedback_tijiao:
                SharedPreferences preferences = getSharedPreferences("gd_amapLocation", Context.MODE_PRIVATE);
                JSONObject object = new JSONObject();
                try {
                    object.put("lng", preferences.getString("longitude", ""));//经度 默认当前请求用户的位置
                    object.put("lat", preferences.getString("latitude", ""));//纬度 默认当前请求用户的位置
                    object.put("is_vip", 0);//是否是vip 0无限制 1会员
                    object.put("is_head", 0);//是否有头像 0无限制 1有头像
                    object.put("is_online", 0);//是否在线 0不限制 1在线
                    object.put("age", list_age);//年龄范围 []不限制 [20,30] 20到30
                    object.put("height", list_height);//身高范围 []不限制 [0,130] 0到130
                    object.put("weight", list_weight);//体重范围 []不限制 [0,130] 0到130
                    object.put("type", int_type);//类型value数组 []不限制 [1,2]
                    object.put("role", int_juese);//角色value数组 []不限制 [1,2]
                    object.put("face", int_tixing);//形象value数组 []不限制 [1,2]
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                postData(object);
                break;
            case R.id.iv_gaoji_shaixuan:
                if (is_gaoji){
                    tv_fill_info2_juese.setVisibility(View.VISIBLE);
                    tv_fill_info2_type.setVisibility(View.VISIBLE);
                    tv_fill_info2_tixing.setVisibility(View.VISIBLE);
                    gv_fill_item_type.setVisibility(View.VISIBLE);
                    gv_fill_item_juese.setVisibility(View.VISIBLE);
                    gv_fill_item_tixing.setVisibility(View.VISIBLE);
                    is_gaoji = false;
                } else {
                    is_gaoji = true;
                    tv_fill_info2_juese.setVisibility(View.GONE);
                    tv_fill_info2_type.setVisibility(View.GONE);
                    tv_fill_info2_tixing.setVisibility(View.GONE);
                    gv_fill_item_type.setVisibility(View.GONE);
                    gv_fill_item_juese.setVisibility(View.GONE);
                    gv_fill_item_tixing.setVisibility(View.GONE);
                }
                break;
            case R.id.back:
                finish();
                break;
            default:
                break;
        }
    }

    /**
     * 网络请求
     */
    private void postData() {
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, RequestConnect.User_Choose_information_Url + access_token + "&client=1", null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.d("tiaojian", jsonObject.toString());
                try {
                    int code = jsonObject.optInt("code");
                    int status = jsonObject.optInt("status");
                    String message = jsonObject.getString("message");
                    if (code == 0) {
                        Gson gson = new Gson();
                        JSONObject info = jsonObject.getJSONObject("info");
                        user_information = gson.fromJson(info.toString(), User_Information.class);
                        fill_information2_adapter = new Fill_Information2_Adapter(TiaoJian_ShaiXuan_Activity.this, user_information, 1);
                        fill_information2_adapter2 = new Fill_Information2_Adapter(TiaoJian_ShaiXuan_Activity.this, user_information, 2);
                        fill_information2_adapter3 = new Fill_Information2_Adapter(TiaoJian_ShaiXuan_Activity.this, user_information, 3);
                        gv_fill_item_type.setAdapter(fill_information2_adapter);
                        gv_fill_item_juese.setAdapter(fill_information2_adapter2);
                        gv_fill_item_tixing.setAdapter(fill_information2_adapter3);
                    } else {
                        ToolUtils.showToast(TiaoJian_ShaiXuan_Activity.this, message);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                ToolUtils.showToast(TiaoJian_ShaiXuan_Activity.this, "网络请求失败，请稍后重试~");
            }
        });
        request.setTag(this);
        VolleyUtil.getQueue(this).add(request);
    }

    /**
     * 网络请求
     * @param object
     */
    private void postData(JSONObject object) {
        Log.d("tiaojian", object.toString());
        SharedPreferences preferences = getSharedPreferences("user_limit", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("object", object.toString());
        editor.apply();
        finish();
    }
}
